package com.dragon.client.controller.avttendance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.avttendance.IAttendanceArrangeService;
import com.dragon.repository.domain.dto.AttendanceArrangeDTO;
import com.dragon.repository.domain.vo.AttendanceArrangeVO;
import com.dragon.repository.entity.AttendanceArrange;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 考勤配置 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-31
 */
@RestController
@RequestMapping("attendanceArrange")
@Api(tags = "考勤主配置")
public class AttendanceArrangeController {

    @Autowired
    IAttendanceArrangeService service;

    @GetMapping("list")
    @ApiOperation(value = "考勤配置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码",required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索条件", required = false, type = "string")
    })
    public ResponseData<List<AttendanceArrangeDTO>> arrangeList(Integer page, Integer limit, String search){
        Page<AttendanceArrange> arrange = service.arrangeList(page, limit, search);
        return ResponseData.success(AttendanceArrangeDTO.convertList(arrange.getRecords()),arrange.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加配置")
    public ResponseData arrangeAdd(@RequestBody @Validated(Insert.class) AttendanceArrangeVO vo){
        Integer arrangeAddId = service.arrangeAdd(AttendanceArrangeVO.convert(vo));
        return ResponseData.success(arrangeAddId);
    }

    @PostMapping("update")
    @ApiOperation(value = "修改配置")
    public ResponseData updateArrange(@RequestBody @Validated(Update.class) AttendanceArrangeVO vo){
        service.updateArrange(AttendanceArrangeVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("seeDetails")
    @ApiOperation(value = "查看详情")
    public ResponseData<AttendanceArrangeDTO> seeDetails(@ApiParam(name = "id", value = "主键id", required = true, type = "int")
                                                         @RequestParam Integer id) {
        AttendanceArrange arrange = service.seeDetails(id);
        return ResponseData.success(AttendanceArrangeDTO.convert(arrange));
    }

    @PostMapping("seeDetailsByAdminId")
    @ApiOperation(value = "根据当前用户查看考勤配置详情")
    public ResponseData<AttendanceArrangeDTO> seeDetailsByAdminId(@ApiParam(name = "adminId", value = "用户id", required = true, type = "int")
                                                         @RequestParam Integer adminId) {
        AttendanceArrange arrange = service.seeDetailsByAdminId(adminId, null);
        return ResponseData.success(AttendanceArrangeDTO.convert(arrange));
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除打卡配置")
    public ResponseData arrangeDelete(@ApiParam(name = "id", value = "主键id", required = true, type = "int")
                                      @RequestParam Integer id){
        service.arrangeDelete(id);
        return ResponseData.success();
    }

}
